package com.alameda.common.base.enums;

import com.alameda.common.base.constant.ProjectConstant;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * 操作结果枚举
 *
 * @author Lv
 * @date 2025/1/4
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum OperateResultEnum {
    /**
     * 操作成功
     */
    SUCCESS(ProjectConstant.OperateInfoPropertyConstant.OPERATE_RESULT_SUCCESS, "操作成功"),

    /**
     * 操作失败
     */
    ERROR(ProjectConstant.OperateInfoPropertyConstant.OPERATE_RESULT_ERROR, "操作失败"),

    ;

    /**
     * 健
     */
    private String key;

    /**
     * 描述
     */
    private String desc;

    /**
     * 通过key获取枚举
     *
     * @param key 键
     * @return
     */
    public static OperateResultEnum getByKey(String key) {
        OperateResultEnum resultEnum = null;
        OperateResultEnum[] operateResultEnums = OperateResultEnum.values();
        for (OperateResultEnum operateResultEnum : operateResultEnums) {
            if (operateResultEnum.key.equals(key)) {
                resultEnum = operateResultEnum;
                break;
            }
        }
        return resultEnum;
    }

    /**
     * 转换为字符串
     *
     * @return
     */
    @Override
    public String toString() {
        return getJsonValue();
    }

    /**
     * 转换为json格式 方便前端渲染格式
     *
     * @return
     */
    @JsonValue
    private String getJsonValue() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("key", this.getKey());
        jsonObject.put("desc", this.getDesc());
        return jsonObject.toJSONString();
    }
}
